package com.simple.piger.controller.company;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.common.collect.Lists;
import com.simple.piger.component.CommonComponent;
import com.simple.piger.constant.AjaxCode;
import com.simple.piger.dto.ComboRespDto;
import com.simple.piger.dto.req.InviteCompanyReqDto;
import com.simple.piger.entity.ApplyUser;
import com.simple.piger.entity.InviteCompanyEmp;
import com.simple.piger.entity.InviteCompanyJob;
import com.simple.piger.service.ApplyUserService;
import com.simple.piger.service.InviteCompanyEmpService;
import com.simple.piger.service.InviteCompanyJobService;
import com.simple.piger.utils.AjaxResult;
import com.simple.piger.utils.DateUtils;
import com.simple.piger.utils.PublicUtil;
import com.simple.piger.controller.BaseController;
import com.simple.piger.dto.resp.company.InviteCompanyRespDto;
import com.simple.piger.entity.InviteCompany;
import com.simple.piger.service.InviteCompanyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 公司表 前端控制器
 * </p>
 *
 * @author baolw
 * @since 2020-02-06
 */
@RestController
@RequestMapping("/inviteCompany")
public class InviteCompanyController extends BaseController {
    @Autowired
    private InviteCompanyService inviteCompanyService;
    @Autowired
    private InviteCompanyJobService inviteCompanyJobService;
    @Autowired
    private ApplyUserService applyUserService;
    @Autowired
    private InviteCompanyEmpService inviteCompanyEmpService;
    @Autowired
    private CommonComponent commonComponent;

    /**
     * 运营平台查看的 公司列表
     * @param hashMap
     * @return
     */
    @GetMapping("/list")
    public AjaxResult list(@RequestParam HashMap hashMap) {
        JSONObject jsonObject = PublicUtil.getJSONObject(hashMap);
        String companyName = jsonObject.getString("companyName");
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        List<InviteCompanyRespDto> list = inviteCompanyService.list(page, limit, companyName);
        int count = inviteCompanyService.count(companyName);
        return success(count, list);
    }

    /**
     * 运营平台查看的 公司详情
     * @param companyId
     * @return
     */
    @GetMapping("/detail/{companyId}")
    public AjaxResult detail(@PathVariable("companyId") String companyId) {
        InviteCompany inviteCompany = inviteCompanyService.selectById(companyId);
        InviteCompanyRespDto dto = new InviteCompanyRespDto();
        BeanUtils.copyProperties(inviteCompany, dto);
        dto.setCreateDate(DateUtils.dateFormat(dto.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
        return success(dto);
    }

    /**
     * 公司查看自己的  信息
     * @return
     */
    @GetMapping("/currentDetail")
    public AjaxResult detail() {
        InviteCompany inviteCompany = inviteCompanyService.selectById(commonComponent.getCompanyIdByCmsUserId(getValueFromSession("userId")));
        InviteCompanyRespDto dto = new InviteCompanyRespDto();
        BeanUtils.copyProperties(inviteCompany, dto);
        dto.setCreateDate(DateUtils.dateFormat(dto.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
        dto.setId(inviteCompany.getId());
        dto.setProvenceCode(dto.getProvinceCode());
        return success(dto);
    }

    /**
     * 更新以及保存公司信息的接口
     * @param hashMap
     * @return
     */
    @PostMapping("/saveOrUpdate")
    public AjaxResult saveOrUpdate(@RequestParam HashMap hashMap) {
        JSONObject jsonObject = PublicUtil.getJSONObject(hashMap);
        InviteCompanyReqDto reqDto = JSONObject.toJavaObject(jsonObject, InviteCompanyReqDto.class);
        inviteCompanyService.saveOrUpdate(reqDto);
        return success();
    }
    @GetMapping("/currentCompany")
    public AjaxResult currentCompany() {
        String userId = getValueFromSession("userId");
        EntityWrapper<ApplyUser> aEw = new EntityWrapper<>();
        aEw.eq("cms_sys_user_id", userId);
        ApplyUser applyUser = applyUserService.selectOne(aEw);

        EntityWrapper<InviteCompanyEmp> empEw = new EntityWrapper<>();
        empEw.eq("apply_user_id", applyUser.getId());
        InviteCompanyEmp emp = inviteCompanyEmpService.selectOne(empEw);

        InviteCompany inviteCompany = inviteCompanyService.selectById(emp.getInviteCompanyId());
        return success(inviteCompany.getCompanyName());
    }

    /**
     * 这个是返回公司下拉选的
     * @return
     */
    @GetMapping("/listSelect")
    public AjaxResult listCompanySelect() {
        EntityWrapper<InviteCompany> ew = new EntityWrapper<>();
        ew.eq("delete_flag", "0");
        List<InviteCompany> list = inviteCompanyService.selectList(ew);
        List<ComboRespDto> results = Lists.newArrayList();
        for (InviteCompany inviteCompany : list) {
            ComboRespDto respDto = new ComboRespDto();
            respDto.setCode(inviteCompany.getId());
            respDto.setValue(inviteCompany.getCompanyName());
            results.add(respDto);
        }
        return success(results);
    }

    /**
     * 这个是删除公司信息的接口
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable("id") String id) {
        InviteCompany inviteCompany = inviteCompanyService.selectById(id);
        EntityWrapper<InviteCompanyJob> ew = new EntityWrapper<>();
        ew.eq("invite_company_id", inviteCompany.getId());
        List<InviteCompanyJob> list = inviteCompanyJobService.selectList(ew);
        for (InviteCompanyJob job : list) {
            job.setDeleteFlag("1");
            job.setUpdateTime(new Date());
            inviteCompanyJobService.updateById(job);
        }
        inviteCompany.setDeleteFlag("1");
        inviteCompany.updateById();
        return success();
    }
}
